package com.gugee.ins.common.dal.service.media.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.media.InsRelMediaCaptionTaggerMapper;
import com.gugee.ins.common.dal.service.media.InsRelMediaCaptionTaggerDao;
import com.gugee.ins.common.model.mysql.media.InsRelMediaCaptionTagger;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

/**
 * insBlogger dao
 * @author Huangxin
 */
@Slf4j
@Service
public class InsRelMediaCaptionTaggerDaoImpl extends ServiceImpl<InsRelMediaCaptionTaggerMapper, InsRelMediaCaptionTagger>
        implements InsRelMediaCaptionTaggerDao {

    /**
     * 批量插入
     * @param shortcode
     * @param taggers
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(String shortcode,long takenTime,List<String> taggers){
        long nowTime = System.currentTimeMillis() / 1000;
        List<InsRelMediaCaptionTagger> insRelMediaCaptionTaggers = new ArrayList<>(taggers.size());
        for(String tagger : taggers){
            InsRelMediaCaptionTagger insRelMediaCaptionTagger = new InsRelMediaCaptionTagger();
            insRelMediaCaptionTagger.setShortcode(shortcode);
            insRelMediaCaptionTagger.setCaptionTaggerName(tagger);
            insRelMediaCaptionTagger.setCreateTime(nowTime);
            insRelMediaCaptionTagger.setTakenTime(takenTime);
            insRelMediaCaptionTaggers.add(insRelMediaCaptionTagger);
        }
        try{
            this.saveBatch(insRelMediaCaptionTaggers,insRelMediaCaptionTaggers.size() + 1);
        }catch (Exception e){
            log.debug("batch insert ins rel media caption tagger failed ,try insert one by one, msg : {} ",e.getMessage());
            insRelMediaCaptionTaggers.forEach(tagger -> {
                try{
                    baseMapper.insert(tagger);
                }catch (Exception ex){
                    log.info("insert ins rel media tagger user failed  ,shortcode : {},captionTaggerName : {}, msg : {} "
                            ,shortcode
                            ,tagger.getCaptionTaggerName(),ex.getMessage());
                }
            });
        }
    }
}
